// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include "OpenMeca/Item/PartUserPipe.hpp"
#include "OpenMeca/Item/Body.hpp"
#include "OpenMeca/Core/System.hpp"
#include "OpenMeca/Core/UserItemCommonProperty.hpp"
#include "OpenMeca/Core/System.hpp"

#include "boost/serialization/export.hpp"
BOOST_CLASS_EXPORT(OpenMeca::Item::PartUserPipe)



namespace OpenMeca
{  
  namespace Item
  {


    
     
    const std::string 
    PartUserPipe::GetStrType()
    {
      return std::string("PartUserPipe");
    }


    void
    PartUserPipe::Init()
    {
      Core::Singleton< Core::UserItemCommonProperty< PartUserPipe > >::Get().CreateAction_Edit();
      Core::Singleton< Core::UserItemCommonProperty< PartUserPipe > >::Get().CreateAction_Delete();
    }



    void 
    PartUserPipe::DrawIcon(QIcon& icon, QColor color)
    {
      //3D ball effect
      const int size = Gui::MainWindow::Get().GetIconSize();
      const int margin = 6;
      QPixmap pixmap(QSize(size,size));
      pixmap.fill(QColor (0, 0, 0, 0));
      QPainter painter(&pixmap);

      const int middle = size/2;

      QRadialGradient gradient(middle, middle, middle, middle, middle);
      gradient.setColorAt(0, Qt::white);
      gradient.setColorAt(1, color);
      QBrush brush(gradient);

      painter.setBrush(brush);
      painter.setPen(Qt::black);

      painter.setBrush(brush);
      painter.setPen(Qt::black);
      painter.drawEllipse(margin, margin, size-2*margin, size-2*margin);
      icon = QIcon(pixmap);
    }


    
    PartUserPipe::PartUserPipe(PartUserPoint& parent)
      :PartUser(GetStrType(), parent),
       startPoint_(parent),
       axis_(1.,0.,0.,boost::bind(&PartUserPipe::GetReferenceFrame, boost::ref(*this))),
       length_(0.)
    {
      
    }

  
    
    
    PartUserPipe::~PartUserPipe()
    {
    }

    const Geom::Frame<_3D>& 
    PartUserPipe::GetReferenceFrame() const
    {
      return GetBody().GetFrame();
    }
	     
    Geom::Vector<_3D>& 
    PartUserPipe::GetAxis()
    {
      return axis_;
    }

    double& 
    PartUserPipe::GetLength()
    {
      return length_;
    }

    PartUserPoint& 
    PartUserPipe::GetStartPoint()
    {
      return startPoint_;
    }

    const PartUserPoint& 
    PartUserPipe::GetStartPoint() const
    {
      return startPoint_;
    }
	     

    void 
    PartUserPipe::BeginDraw()
    {
      Geom::Point<_3D>& p = PartUser::GetPoint();
      p = Geom::Point<_3D>(GetStartPoint().GetPoint(),
			   boost::bind(&PartUserPipe::GetReferenceFrame, boost::ref(*this)));

      Geom::Quaternion<_3D>& q = PartUser::GetQuaternion();
      const Geom::Vector<_3D>& X = GetReferenceFrame().GetXAxis();
      q.SetVecFromTo(X,axis_);
    }

    void 
    PartUserPipe::DrawShape()
    {
      const double scale = Core::System::Get().GetSettings().GetScalePart()/100.;
      Core::Drawable::DrawCylinder(.01f*scale, length_, false);
    }

    void 
    PartUserPipe::UpdateIcon()
    {
      PartUserPipe::DrawIcon(GetIcon(), GetColor().GetQColor());
    }
    

  }
}


namespace OpenMeca
{  
  namespace Core
  {

    template<>
    void
    ItemCommonProperty<OpenMeca::Item::PartUserPipe>::BuildIconSymbol()
    {
      QIcon icon;
      OpenMeca::Item::PartUserPipe::DrawIcon(iconSymbol_, Qt::gray);
    }



  }
}


